﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SQLite;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MarkDownFileSplitter.Meilisearch
{
    public partial class FormOpenSqlite : Form
    {
        public JArray Result { get; private set; }
        public string TableName { get; private set; }

        public FormOpenSqlite()
        {
            InitializeComponent();
        }

        private void button_selectDBFile_Click(object sender, EventArgs e)
        {
            // 打开文件选择对话框
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Filter = "SQLite数据库文件|*.db;*.sqlite|所有文件|*.*";
                openFileDialog.Title = "选择SQLite数据库文件";

                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    this.textBox_dbPath.Text = openFileDialog.FileName;
                    this.comboBox_tableName.Items.Clear();

                    // 构建数据库连接字符串
                    string dbPath = $"Data Source={openFileDialog.FileName};Version=3;";

                    // 创建连接
                    using (SQLiteConnection conn = new SQLiteConnection(dbPath))
                    {
                        conn.Open();

                        // 执行SQL语句获取所有表名
                        string sql = "SELECT name FROM sqlite_master WHERE type='table'";
                        using (SQLiteCommand cmd = new SQLiteCommand(sql, conn))
                        {
                            using (SQLiteDataReader reader = cmd.ExecuteReader())
                            {
                                while (reader.Read())
                                {
                                    // 读取表名
                                    string tableName = reader["name"].ToString();
                                    TableName = tableName;
                                    this.comboBox_tableName.Items.Add(tableName);
                                }
                            }
                        }
                    }

                    this.comboBox_tableName.SelectedIndex = 0;
                }
            }
        }

        private void button_connect_Click(object sender, EventArgs e)
        {
            string dbPath = $"Data Source={this.textBox_dbPath.Text};Version=3;";
            using (SQLiteConnection conn = new SQLiteConnection(dbPath))
            {
                conn.Open();

                string tableName = this.comboBox_tableName.Text;
                // 创建JArray用于存储所有行数据
                Result = new JArray();

                // 查询表中所有数据
                string sql = $"SELECT * FROM {tableName}";
                using (SQLiteCommand cmd = new SQLiteCommand(sql, conn))
                {
                    using (SQLiteDataReader reader = cmd.ExecuteReader())
                    {
                        // 获取所有列名
                        var columnNames = Enumerable.Range(0, reader.FieldCount)
                            .Select(i => reader.GetName(i))
                            .ToList();

                        // 读取每一行数据
                        while (reader.Read())
                        {
                            // 为每行创建一个JObject
                            JObject rowObject = new JObject();

                            // 遍历所有列
                            foreach (string columnName in columnNames)
                            {
                                // 将列名和对应的值添加到JObject中
                                object value = reader[columnName];
                                rowObject[columnName] = value == DBNull.Value ? null : JToken.FromObject(value);
                            }

                            // 将该行数据添加到结果数组中
                            Result.Add(rowObject);
                        }
                    }
                }
            }

            this.DialogResult = DialogResult.OK;
        }
    }
}
